import { Component, OnInit,ChangeDetectorRef } from '@angular/core';
import { Router ,ActivatedRoute} from '@angular/router'; //导入router服务
import { CommonService } from '../../service/common.service'
import { StorageService } from "../../service/storage.service";
import { EventService } from '../../service/event.service'


@Component({
  selector: 'app-setpwd',
  templateUrl: './setpwd.page.html',
  styleUrls: ['./setpwd.page.scss'],
})
export class SetpwdPage implements OnInit {

  // 定义 输入内容
  public phone:string;
  public yzm:string = ''
  public flagYzm:boolean = true;
  public times:any = 60;

  public passworda:any;
  public passwordb:any;

  public sessionId:any='';

 public flagmm:boolean = false;
 public title:string = '';
 public begin:any;
 public show:boolean=false;

 public PersonalInfo:any
 
 constructor(public eventService:EventService,private common:CommonService,private storage:StorageService,private router:Router,private activatedRoute:ActivatedRoute,
   private changeDetectorRef: ChangeDetectorRef) { }
 

 ngOnInit() {
  this.PersonalInfo = this.storage.get('PersonalInfo')

   this.activatedRoute.url.subscribe(params => {
     // console.log(params)
     this.flagYzm = true;
     this.times = 60;
   });

   this.phone=this.PersonalInfo.phone;

   console.log()
   if(this.PersonalInfo.paymentCode == "" || this.PersonalInfo.paymentCode == undefined){
     this.flagmm = false
     this.title = "密码设置"
   }else{
     this.flagmm = true
     this.title = "重置支付密码"
   }
 }

//  ionViewDidEnter(){
//    this.flagYzm = true;
//    this.times = 60
//    this.yzm = ''
//    window.clearInterval(this.begin)
//  }

 // 验证码
 getYzm(){
   
   // alert(this.times)
   // 校验手机
   if(this.common.yzShouJi(this.phone) == false){
     return false
   }
   this.times = 60;
   // 60s倒计时
   this.flagYzm = false
   this.show=true
   this.changeDetectorRef.detectChanges()
   this.begin = setInterval(()=>{   
       this.times--;
       if(this.times == 0){ 
           this.times = 60;
           this.flagYzm = true                     
           clearInterval(this.begin);
       }
       // 浏览器不更新页面
       this.changeDetectorRef.detectChanges()
   },1000)
   
   // 传参数
   var api = '/tbr/sendMessage'
   var params = {'phone': this.phone}
   
   this.common.post(api,params,true).then((response:any)=>{
    
     if(response.code == '200'){
       this.show=false
       this.sessionId  = response.data.sessionId
       // console.log(response)
     }
   })
 }

 events(){
  this.eventService.event.emit('useraction');
 }

 // 设置密码
 onSure(){
   if(this.passworda != this.passwordb){
     this.common.presentAlert("密码不一致")
     return false
   }
   if(this.passworda.length < 6){
     this.common.presentAlert("密码为6位数")
     return false
   }

   this.show=true
   // 设置密码

   if(this.flagmm == false ){
     var api = '/tbr/setPaymentCode'
     var params = {
       paymentCode:this.passworda,
       userId:this.PersonalInfo.userId
     }
     this.common.post(api,params,false).then((res:any)=>{
       console.log(res)
       if(res.code == 200){
        this.show=false
        //通知用户中心更新用户信息
        this.events()
        this.common.presentToast(res.msg)
        this.router.navigateByUrl('/tabs/tab4')
       }
     })
   }else{
     // 修改支付密码
     if(this.yzm == ''){
       this.common.presentAlert("验证码不能为空")
        return false
     }

     var api = '/tbr/changePaymentCode'
     var paramsa = {
       phone:this.phone,
       code:this.yzm,
       passWord:this.passworda,
       sessionId:this.sessionId
     }
     // console.log(paramsa)
     this.common.post(api,paramsa,false).then((res:any)=>{
       if(res.code == 200){
          this.router.navigateByUrl('/tabs/tab4')
       }
       this.show=false
     })

   }
   
 }

}
